package com.reds.backend.service.impl.article;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reds.backend.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.reds.backend.service.article.articleGetService;
import com.reds.backend.mapper.articleMapper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class articleGetServiceImpl implements articleGetService{
    @Autowired
    articleMapper articleMapper;
    @Override
    public String getArticle(int id) {
        Article article = articleMapper.selectById(id);
        if(article == null) return "error";
        else return JSON.toJSONString(articleMapper.selectById(id));
    }

    @Override
    public String isMyArticle(int id,String author) {
        Article article = articleMapper.selectById(id);
        if(article.getAuthor().equals(author))
            return "success";
        else return "error";
    }

    @Override
    public JSONObject getArticleByTag(String tag, int page, int sizes) {
            IPage<Article> iPage = new Page<>(page,sizes);
            QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("tag",tag).orderByDesc("id");
            List<Article> list = articleMapper.selectPage(iPage,queryWrapper).getRecords();
            JSONObject jsonObject = new JSONObject();
        jsonObject.put("article",list);
        jsonObject.put("length",articleMapper.selectCount(queryWrapper));
        return jsonObject;
    }

    @Override
    public List<Article> getLatestArticle(int id) {
        QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();
        articleQueryWrapper.orderByDesc("time").last("limit 5").ne("id",id);
        return articleMapper.selectList(articleQueryWrapper);
    }

    @Override
    public List<Article> getRelatedArticle(String tag,int id) {
        QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();
        articleQueryWrapper.orderByDesc("time").last("limit 5").eq("tag",tag).ne("id",id);
        return articleMapper.selectList(articleQueryWrapper);
    }

    @Override
    public Article nextArticle(int id,String tag) {
        //查询下一个
        QueryWrapper<Article> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.orderByAsc("id").gt("id",id).last("limit 1").eq("tag",tag);
        List<Article> list = articleMapper.selectList(queryWrapper1);
        if(list.isEmpty()) return null;
        else return articleMapper.selectList(queryWrapper1).get(list.size()-1);
    }
    @Override
    public Article previousArticle(int id,String tag) {
        //查询下一个
        QueryWrapper<Article> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.orderByAsc("id").lt("id",id).eq("tag",tag);
        List<Article> list = articleMapper.selectList(queryWrapper1);
        if(list.isEmpty()) return null;
        else return articleMapper.selectList(queryWrapper1).get(list.size()-1);
    }
    @Override
    public List<Article> getArticleByTagHome(String tag) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        // 添加查询条件，查询 state 字段值为 null 的记录
        queryWrapper.isNull("state");
        List<Article> list = articleMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public JSONObject getArticleByAuthor(String name,int page,int sizes) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("author",name).orderByDesc("id");
        IPage<Article> iPage = new Page<>(page,sizes);
        List<Article> list = articleMapper.selectPage(iPage,queryWrapper).getRecords();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("article",list);
        jsonObject.put("length",articleMapper.selectCount(queryWrapper));
        return jsonObject;
    }


}
